/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// var searchInsert = function (nums, target) {
//   let left = 0,
//     right = nums.length,
//     ans = nums.length;
//   while (left <= right) {
//     const mid = (left + right) >> 1;
//     if (target <= nums[mid]) {
//       ans = mid;
//       right = mid - 1;
//     } else {
//       left = mid + 1;
//     }
//   }
//   return ans;
// };

var searchInsert = function (nums, target) {
  let res = 0;
  const tool = (left, right) => {
    const mid = (left + right) >> 1;
    if (nums[mid] > target) {
      tool(left, mid - 1);
    } else if (nums[mid] < target) {
      tool(mid + 1, right);
    } else {
      res = mid;
    }
  };
  tool(0, nums.length);
  return res;
};

console.log(searchInsert([1, 3, 5, 6], 5));
